package com.huipinche.view.impl;

import com.huipinche.view.RegisterPageView;
import com.huipinche.view.utils.*;


import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.huipinche.service.impl.*;

public class registrationPage implements RegisterPageView {
    private JFrame jf;
    final int WIDTH = 600;
    final int HEIGHT = 600;
    private int page = 0;
    private boolean flag = true;
   // private boolean isleep = false;

    /*public static void main(String[] args) throws IOException {
        registrationPage window = null;
        window = new registrationPage();
       // window.jf.setVisible(true);
    }*/


    private void initialize() throws IOException {
        jf = new JFrame("注册界面");

        jf.setBounds((ScreenUtils.getScreenWidth()-WIDTH)/2,(ScreenUtils.getScreenHeight()-HEIGHT)/2,WIDTH,HEIGHT);

        jf.setIconImage(ImageIO.read(new File(PathUtils.getRealPath("logo3.jpeg"))));

        Image image=new ImageIcon(PathUtils.getRealPath("bigbackground2.png")).getImage();
        BackGroundPanel bgPanel = new BackGroundPanel(image);
        bgPanel.setBounds(0,0,WIDTH,HEIGHT);

        Box vBox = Box.createVerticalBox();

        //组装用户名
        Box uBox = Box.createHorizontalBox();
        JLabel usernameLabel = new JLabel("用户名: ");
        usernameLabel.setFont(new Font("楷体",Font.BOLD,18));//设置字体大小
        usernameLabel.setForeground(Color.white);//设置字体颜色

        JTextField usernameField = new JTextField();
        usernameField.setPreferredSize(new Dimension (150,30));//设置宽高
        //usernameField.setOpaque(false);//设置透明

        JTextArea sloganArea = new JTextArea("欢迎注册惠拼车\n今天拼车人，明日人上人。", 3, 25);

        sloganArea.setOpaque(false);//设置文本域为透明
        sloganArea.setForeground(Color.white);//设置字体颜色
        sloganArea.setFont(new Font("楷体",Font.BOLD,30));//设置字体大小
        sloganArea.setEditable(false);//设置文本域不能被更改

        //uBox.add(sloganArea);

        uBox.add(usernameLabel);
        uBox.add(Box.createHorizontalStrut(10));
        uBox.add(usernameField);

        //组装密码
        Box pBox = Box.createHorizontalBox();
        JLabel passwordLabel = new JLabel("密 码:");
        passwordLabel.setFont(new Font("楷体",Font.BOLD,18));//设置字体大小
        passwordLabel.setForeground(Color.white);//设置字体颜色

        JPasswordField passwordField = new JPasswordField();
        passwordField.setPreferredSize(new Dimension (150,30));//设置宽高
       // passwordField.setOpaque(false);//设置透明

        pBox.add(passwordLabel);
        pBox.add(Box.createHorizontalStrut(20));
        pBox.add(passwordField);


        JTextField out = new JTextField("注册状态:", 32);
        out.setFont(new Font("楷体",Font.BOLD,18));//设置字体大小
        out.setForeground(Color.white);//设置字体颜色
        out.setOpaque(false);

        //组装手机号
        Box tBox = Box.createHorizontalBox();
        JLabel telLabel = new JLabel("+86 ∨");
        telLabel.setFont(new Font("楷体",Font.BOLD,18));//设置字体大小
        telLabel.setForeground(Color.white);//设置字体颜色


        JTextField telField = new JTextField(32);
        telField.setPreferredSize(new Dimension (150,28));//设置宽高
        //telField.setOpaque(false);//设置透明

        tBox.add(telLabel);
        tBox.add(Box.createHorizontalStrut(20));
        tBox.add(telField);

        //组装同意注册条款
        Box aBox = Box.createHorizontalBox();
        JCheckBox agreeBox = new JCheckBox("我已阅读并同意相关服务条款和隐私政策  ∨              ");
        //JTextField agreeField = new JTextField("我已阅读并同意相关服务条款和隐私政策∨", 28);
        agreeBox.setSelected(false);
        //agreeBox.setOpaque(false);//设置透明
        JButton registrationButton = new JButton("注册");
        JButton backloginButton = new JButton("返回登录");

        aBox.add(agreeBox);
        aBox.add(Box.createHorizontalStrut(20));
        aBox.add(registrationButton);
        aBox.add(Box.createHorizontalStrut(10));
        aBox.add(backloginButton);



        JLabel explainLabel = new JLabel("可以通过手机号找回密码" );
        explainLabel.setFont(new Font("楷体",Font.BOLD,18));//设置字体大小
        explainLabel.setForeground(Color.white);//设置字体颜色
        //explainLabel.setOpaque(true);


        vBox.add(sloganArea);
        vBox.add(Box.createVerticalStrut(40));
        vBox.add(uBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(pBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(out);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(tBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(explainLabel);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(aBox);


        bgPanel.add(vBox);
        jf.add(bgPanel);



        registrationButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {


                 if(new registerUserServiceimpl().registerUser(usernameField.getText(),
                         passwordField.getText(),telField.getText())){
                     new GeneralPageimpl("注册成功");
                  //注册成功，返回登录
                 }else{
                     new GeneralPageimpl("注册失败，请重新注册");//注册失败重新，注册
                 }


            }
        });
        registrationPage temp = this;
        backloginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                temp.page = 110;//登录页面
                temp.flag = false;
            }
        });

        //registrationButton.setSize(20, 20);
        jf.setResizable(false);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }

   /* private boolean judgeInput(String username, String password, String telePhone) {
        String regex = "[1][34578][0-9]{9}";
        int flag = 0;
        boolean f = false;
        boolean g = false;

            *//**
             * 检测username
             *//*
           if(username!=null) {
               for (int j = 0; j < username.length(); j++) {
                   char a = username.charAt(j);
                   if (!(a >= 'A' && a <= 'z' || a >= '0' && a <= '9' || a == '_')) {
                       //判断符合这种情况的然后取反，不满足这种情况的都是错误的
                       f = false;

                   } else {
                       f = true;
                       flag++;
                   }
               }
               if (f) {
                   System.out.println("格式正确");
               } else {
                  // new generalFrame("用户名格式错误,请输入字母加数字的用户名");
                   System.out.println("格式错误");
                   return f;
               }
           }else {
            //   new generalFrame("用户名不能为空");
               return false;
           }

            *//**
             * 检测password
             *//*
           if(password!=null) {
               for (int i = 0; i < password.length(); i++) {
                   char a = username.charAt(i);
                   if (!(a >= 'A' && a <= 'z' || a >= '0' && a <= '9' || a == '_')) {//判断符合这种情况的然后取反，不满足这种情况的都是错误的
                       f = false;

                   } else {
                       f = true;
                       flag++;
                   }
               }
               if (f) {
                   System.out.println("格式正确");
               } else {
                 //  new generalFrame("密码格式错误，请输入字母加数字的密码");
                   System.out.println("格式错误");
                   return false;
               }
           }else {
               //new generalFrame("密码不能为空");
               return false;

           }

            *//**
             * 检测telePhone
             *//*
             if(telePhone!=null) {
                 if (telePhone.matches(regex)) {
                     System.out.println("电话号码格式正确");
                     flag++;
                 } else {
                  //   new generalFrame("电话号码格式不正确");
                     System.out.println("电话号码格式不正确");
                     return false;
                 }

                 if (flag == 3) {

                     return true;
                 }
             }else {
                // new generalFrame("电话号码不能为空");

                 return false;
             }

                 return true;

    }
*/
    @Override
    public int RegisterPage(boolean isleep) throws Exception{
        this.flag = isleep;
            initialize();
        while(this.flag){
            Thread.sleep(200);
        };
            switch (page){
                case 110:
                    jf.removeNotify();
                    return 110;//返回登录
            }

        return 0;
    }
}
